package com.sctu.http;


import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Administrator on 2015/12/4.
 */
public class GetJson {

        private static GetJson getJson;
       public static GetJson install(){
        if (getJson == null){
            getJson = new GetJson();
        }
           return getJson;
    }

        /**
         * get方式获取json数据
         *
         * @return
         * @throws IOException
         */
        public static String requestByGet(String url) throws IOException {
            String json = "";
            URL url1 = new URL(url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url1.openConnection();
            httpURLConnection.setConnectTimeout(10* 1000);
            httpURLConnection.connect();

            byte[] bytes = new byte[1024];
            int reader = 0;
            if (httpURLConnection.getResponseCode() == 200) {
                InputStream inputStream = httpURLConnection.getInputStream();
                while ((reader = inputStream.read(bytes)) != -1) {
                    json += new String(bytes, 0, reader);
                }
            }

//        Bitmap bitmap =  BitmapFactory.decodeStream(httpURLConnection.getInputStream());

            httpURLConnection.disconnect();
//        bitmap.recycle();
//        bitmap = null;
            return json;
        }




        // Post方式请求
        public static String requestByPost() throws Exception {
            String string = "";
            String path = "http://192.168.253.1:8080/it/android/upload";
            // 请求的参数转换为byte数组
            String params = "workorder_description=dasdasdasdasd";
            byte[] postData = params.getBytes();

            URL url = new URL(path);
            // 打开HttpURLConnection连接
            HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
            // 设置连接超时时间
            urlConn.setConnectTimeout(5 * 1000);
            // Post请求必须设置允许输出
            urlConn.setDoOutput(true);
            // Post请求不能使用缓存
            urlConn.setUseCaches(false);
            // 设置为Post请求
            urlConn.setRequestMethod("POST");
            urlConn.setInstanceFollowRedirects(true);
            // 配置请求Content-Type
            urlConn.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencode");
            // 开始连接
            urlConn.connect();
            // 发送请求
            DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());
            dos.write(postData);
            dos.flush();
            dos.close();
            byte[] bt = new byte[1024];
            int reader = 0;
            if (urlConn.getResponseCode() == 200) {
                InputStream inputstream = urlConn.getInputStream();
                while ((reader = inputstream.read(bt)) != -1) {
                    string += new String(bt, 0, reader);
                }
            } else {

            }
            return string;
        }
}
